package com.plum.admin.modules.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plum.admin.common.annotation.Log;
import com.plum.admin.common.enums.LogType;
import com.plum.admin.common.util.R;
import com.plum.admin.modules.sys.entity.SysJob;
import com.plum.admin.modules.sys.service.ISysJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 岗位管理 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/sys/job")
@Api(tags = "岗位管理")
@RequiredArgsConstructor
public class SysJobController extends BaseController {

    private final ISysJobService jobService;

    @ApiOperation("查询岗位列表")
    @GetMapping("getJobList")
    @Log(type = LogType.OPERATION,title = "查询岗位列表")
    public R<Object> getJobList(){
        return R.ok(jobService.list());
    }

    @ApiOperation("分页查询岗位列表")
    @GetMapping
    @PreAuthorize("hasAnyAuthority('sys:job:list')")
    @Log(type = LogType.OPERATION,title = "分页查询岗位列表")
    public R<Object> list(Integer page, Integer pageSize, SysJob job){
        LambdaQueryWrapper<SysJob> queryWrapper = Wrappers.<SysJob>query().lambda()
                .eq(StrUtil.isNotBlank(job.getDeptId()), SysJob::getDeptId, job.getDeptId())
                .like(StrUtil.isNotBlank(job.getJobName()), SysJob::getJobName, job.getJobName());
        return R.ok(jobService.queryJobList(new Page<>(page,pageSize),queryWrapper));
    }
    @ApiOperation("添加岗位")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:job:insert')")
    @Log(type = LogType.OPERATION,title = "添加岗位")
    public R<Object> saveJob(@RequestBody SysJob job){
        return R.ok(jobService.save(job));
    }

    @ApiOperation("修改岗位")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:job:update')")
    @Log(type = LogType.OPERATION,title = "修改岗位")
    public R<Object> updateJob(@RequestBody SysJob job){
        return R.ok(jobService.updateById(job));
    }

    @ApiOperation("删除岗位")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('sys:job:delete')")
    @Log(type = LogType.OPERATION,title = "删除岗位")
    public R<Object> deleteJob(String ids){
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(jobService.removeByIds(idList));
    }
}
